package com.climber.android.lib_share.platform.wechat

import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.ShareSDK
import cn.sharesdk.wechat.friends.Wechat
import com.climber.android.lib_share.entity.ShareConfig
import com.climber.android.lib_share.sensor.MyShareActionListener
import com.climber.android.lib_share.sensor.ShareAction
import com.climber.android.lib_share.sensor.ShareResultCallBack
import com.mob.MobSDK

/**
 * Created by Mikes at 2020-03-19 16:25
 * 微信好友分享
 */
class WeChatShare internal constructor(val shareConfig : ShareConfig, val builder: ShareResultCallBack.Builder.()->Unit): ShareAction {
    // 分享纯文字
    override fun shareText() {
        val shareParams = Platform.ShareParams()
        shareParams.shareType = Platform.SHARE_TEXT
        shareParams.title = shareConfig.title
        shareParams.text = shareConfig.text

        val platform = ShareSDK.getPlatform(Wechat.NAME)
        platform.platformActionListener = MyShareActionListener(builder)

        //启动分享
        platform.share(shareParams)
    }

    // 分享图片
    override fun shareImage(){
        val shareParams = Platform.ShareParams()
        shareParams.shareType = Platform.SHARE_IMAGE
        shareParams.title = shareConfig.title
        shareParams.text = shareConfig.text
        shareParams.imageUrl = shareConfig.imageUrl ?: ""
        shareParams.imagePath = shareConfig.imagePath ?: ""
        shareParams.imageData = shareConfig.imageBmp

        val platform = ShareSDK.getPlatform(Wechat.NAME)
        platform.platformActionListener = MyShareActionListener(builder)

        //启动分享
        platform.share(shareParams)
    }

    override fun shareMusic(){
        val shareParams = Platform.ShareParams()
        shareParams.shareType = Platform.SHARE_MUSIC
        shareParams.title = shareConfig.title
        shareParams.text = shareConfig.text
        // 消息点击后打开的页面
        shareParams.url = shareConfig.url ?: ""
        shareParams.imageUrl = shareConfig.imageUrl ?: ""
        shareParams.imagePath = shareConfig.imagePath ?: ""
        shareParams.imageData = shareConfig.imageBmp
        shareParams.musicUrl = shareConfig.musicUrl ?: ""

        val platform = ShareSDK.getPlatform(Wechat.NAME)
        platform.platformActionListener = MyShareActionListener(builder)

        //启动分享
        platform.share(shareParams)
    }

    override fun shareVideo(){
        val shareParams = Platform.ShareParams()
        shareParams.shareType = Platform.SHARE_VIDEO
        shareParams.title = shareConfig.title
        shareParams.text = shareConfig.text
        shareParams.imageUrl = shareConfig.imageUrl ?: ""
        shareParams.imagePath = shareConfig.imagePath ?: ""
        shareParams.imageData = shareConfig.imageBmp
        // 视频网页地址
        shareParams.url = shareConfig.url ?: ""

        val platform = ShareSDK.getPlatform(Wechat.NAME)
        platform.platformActionListener = MyShareActionListener(builder)

        //启动分享
        platform.share(shareParams)
    }

    override fun shareWebPage(){
        val shareParams = Platform.ShareParams()
        shareParams.shareType = Platform.SHARE_WEBPAGE
        shareParams.title = shareConfig.title
        shareParams.text = shareConfig.text
        shareParams.url = shareConfig.url ?: ""
        shareParams.imageUrl = shareConfig.imageUrl ?: ""
        shareParams.imagePath = shareConfig.imagePath ?: ""
        shareParams.imageData = shareConfig.imageBmp

        val platform = ShareSDK.getPlatform(Wechat.NAME)
        platform.platformActionListener = MyShareActionListener(builder)

        //启动分享
        platform.share(shareParams)
    }

    override fun shareFile() {
        val shareParams = Platform.ShareParams()
        shareParams.shareType = Platform.SHARE_FILE
        shareParams.title = shareConfig.title
        shareParams.text = shareConfig.text
        shareParams.imageUrl = shareConfig.imageUrl ?: ""
        shareParams.imagePath = shareConfig.imagePath ?: ""
        shareParams.imageData = shareConfig.imageBmp
        shareParams.filePath = shareConfig.filePath ?: ""

        val platform = ShareSDK.getPlatform(Wechat.NAME)
        platform.platformActionListener = MyShareActionListener(builder)

        //启动分享
        platform.share(shareParams)
    }

    override fun shareMiniProgram(){
        val shareParams = Platform.ShareParams()
        shareParams.shareType = Platform.SHARE_WXMINIPROGRAM
        shareParams.wxUserName = shareConfig.wxUserName ?: ""
        shareParams.wxPath = shareConfig.wxPath ?: ""
        shareParams.title = shareConfig.title
        shareParams.text = shareConfig.text
        shareParams.url = shareConfig.url ?: ""
        shareParams.imageUrl = shareConfig.imageUrl ?: ""
        shareParams.imagePath = shareConfig.imagePath ?: ""
        shareParams.imageData = shareConfig.imageBmp

        val platform = ShareSDK.getPlatform(Wechat.NAME)
        platform.platformActionListener = MyShareActionListener(builder)

        //启动分享
        platform.share(shareParams)
    }
}